package com.syslog;

import java.io.BufferedReader;   

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;   
import java.io.InputStreamReader;  
import java.io.LineNumberReader;


public class GetMac {
	private static final GetMac instance = new GetMac();
	private GetMac ()
	{	
	
	}
	public static GetMac getInstance ()
	{
		return instance;
	}
	
	 private static String getOSName() {   
	        return System.getProperty("os.name").toLowerCase();   
	    }   
	       
	 /**  
	     * 获取unix网卡的mac地址.  
	     * 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.  
	     * @return mac地址  
	     */  
	 public static String getUnixMACAddress() {   
	        String mac = null;   
	        BufferedReader bufferedReader = null;   
	        Process process = null;   
	        try {   
	            process = Runtime.getRuntime().exec("ifconfig eth0");
	            bufferedReader = new BufferedReader(new InputStreamReader(process   
	                    .getInputStream()));   
	            String line = null;   
	            int index = -1;   
	            while ((line = bufferedReader.readLine()) != null) {   
	                index = line.toLowerCase().indexOf("hwaddr");
	                if (index >= 0) {
	                    mac = line.substring(index +"hwaddr".length()+ 1).trim();
	                    break;   
	                }   
	            }   
	        } catch (IOException e) {   
	            e.printStackTrace();   
	        } finally {   
	            try {   
	                if (bufferedReader != null) {   
	                    bufferedReader.close();   
	                }   
	            } catch (IOException e1) {   
	                e1.printStackTrace();   
	            }   
	            bufferedReader = null;   
	            process = null;   
	        }   
	  
	        return mac;   
	    }   
	  
	  /**  
	     * 获取widnows网卡的mac地址.  
	     * @return mac地址  
	     */  
	 public static String getWindowsMACAddress() {   
	        String mac = null;   
	        BufferedReader bufferedReader = null;   
	        Process process = null;   
	        try {
	            process = Runtime.getRuntime().exec("ipconfig /all");
	            bufferedReader = new BufferedReader(new InputStreamReader(process   
	                    .getInputStream()));   
	            String line = null;   
	            int index = -1;   
	            while ((line = bufferedReader.readLine()) != null) {   
	                index = line.toLowerCase().indexOf("物理地址");
	                if (index >= 0) {
	                    index = line.indexOf(":");
	                    if (index>=0) {   
	                        mac = line.substring(index + 1).trim();
	                    }   
	                    break;   
	                }   
	            }   
	        } catch (IOException e) {   
	            e.printStackTrace();   
	        } finally {   
	            try {   
	                if (bufferedReader != null) {   
	                    bufferedReader.close();   
	                }   
	            } catch (IOException e1) {   
	                e1.printStackTrace();   
	            }   
	            bufferedReader = null;   
	            process = null;   
	        }   
	  
	        return mac;   
	    }   
	 
	 
	  /**  
	     * 获取widnows网卡的mac地址. (客户端) 
	     * @return mac地址  
	     */  
	 public static String getWindowsMACAddressClint(String ip) {   
		 String str = "";   
		 String macAddress = "";   
		 try {   
			Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);   
			InputStreamReader ir = new InputStreamReader(p.getInputStream());   
		    LineNumberReader input = new LineNumberReader(ir);   
		    for (int i = 1; i < 100; i++) {   
		    	str = input.readLine();   
		    	if (str != null) {   
		    		if (str.indexOf("MAC") > 1) {   
		    			macAddress = str.substring(str.indexOf("MAC") + 14, str.length());   
		    			break;
		    		}
		    	}   
		    }   
		 } catch (IOException e) {   
			e.printStackTrace(System.out);   
		 }   
		return macAddress; 
	}   
	 
}
